package com.example.paichaapp;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;

import android.Manifest;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.view.MenuItem;
import android.widget.Toast;

import com.example.paichaapp.Util.Util;
import com.example.paichaapp.adapter.MainFragmentAdapter;
import com.example.paichaapp.fragment.ControlFragment;
import com.example.paichaapp.fragment.EquipmentFragment;
import com.example.paichaapp.fragment.MineFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.tencent.mmkv.MMKV;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private AlarmService.AlarmBinder alarmBinder;
    private int isAlarmOk = 0;
    private String cause;

    private ViewPager2 viewPager;
    private BottomNavigationView bottomNavigationView;
    private List<Fragment> fragmentList =new ArrayList<>();
    ActivityResultLauncher activityResultLauncher;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getReadPermissions();

        MMKV.initialize(this);
        viewPager=findViewById(R.id.main_vp2);
        bottomNavigationView=findViewById(R.id.bottom_navigation);
        initBottomNavigation();
        Intent startServiceIntent = new Intent(MainActivity.this, AlarmService.class);
        startService(startServiceIntent);
        Intent bindIntent = new Intent(MainActivity.this,AlarmService.class);
        bindService(bindIntent,alarmConnection, Context.BIND_AUTO_CREATE);   //绑定服务


    }

    @Override
    protected void onStart() {
        super.onStart();
        Util util=new Util();
    }

    private final ServiceConnection alarmConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            alarmBinder = (AlarmService.AlarmBinder) iBinder;
            alarmBinder.setActivity(MainActivity.this);
//            alarmBinder.setServe();
//            while (true) {
//                if (isAlarmOk!=0){break;}
//                isAlarmOk = alarmBinder.Alarm();
//                cause = alarmBinder.Cause();
//            }
//            isAlarmOk = 0;
//            MMKV.initialize(MainActivity.this);
//            MMKV mmkv = MMKV.mmkvWithID("contactPerson");
//            int i = 1;
//            for ( ; mmkv.decodeString("person" + i) != null; i++) {
//                if (mmkv.decodeString("switch" + i).equals("on")) {
//                    Intent intent = new Intent();
//                    intent.setAction(intent.ACTION_SENDTO);
//                    intent.setData(Uri.parse("smsto:" + mmkv.decodeString("phone" + i)));
//                    intent.putExtra("sms_body","警报！排插端口异常！");
//                    startActivity(intent);
//                }
//            }
//            AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
//            dialog.setTitle("警报");
//            dialog.setMessage("收到端口警报：" + cause);
//            dialog.setCancelable(false);
//            dialog.setPositiveButton("确认", new DialogInterface.OnClickListener() {
//                @Override
//                public void onClick(DialogInterface dialogInterface, int i) {
//                }
//            });
//            dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
//                @Override
//                public void onClick(DialogInterface dialogInterface, int i) {
//                }
//            });
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
        }
    };

    //刷新每次传入的Intent
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
    }



    private void initBottomNavigation() {
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){

                    case R.id.chat:
                        viewPager.setCurrentItem(0);

                        break;
                    case R.id.account:
                        viewPager.setCurrentItem(1);

                        break;
                    case R.id.setting:
                        viewPager.setCurrentItem(2);

                        break;
                    default:break;

                }

                return false;
            }
        });
        viewPager.setAdapter(new MainFragmentAdapter(MainActivity.this));
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                bottomNavigationView.getMenu().getItem(position).setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });
    }
    public  void  getReadPermissions(){
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS)
                != PackageManager.PERMISSION_GRANTED) {
            //未赋予权限，申请权限
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.SEND_SMS)) {
                //选择不开启权限的时候，提示用户
                Toast.makeText(this, "请开通相关权限，否则无法正常使用本应用！", Toast.LENGTH_SHORT).show();
            }
            //申请权限
            ActivityCompat.requestPermissions(this,  new String[]{Manifest.permission.SEND_SMS}, 10001);

        } else {
            //权限已赋予
            Toast.makeText(this, "已授权成功！", Toast.LENGTH_SHORT).show();
        }}

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 10001:
                for (int i = 0; i < grantResults.length; i++) {
//                   如果拒绝获取权限
                    if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                        //判断是否勾选禁止后不再询问
                        boolean flag = ActivityCompat.shouldShowRequestPermissionRationale(this, permissions[i]);
                        if (flag) {
                            getReadPermissions();
                            return;//用户权限是一个一个的请求的，只要有拒绝，剩下的请求就可以停止，再次请求打开权限了
                        } else { // 勾选不再询问，并拒绝
                            Toast.makeText(this, "请到设置中打开权限", Toast.LENGTH_LONG).show();
                            return;
                        }
                    }
                }
//                Toast.makeText(LoginActivity.this, "权限开启完成",Toast.LENGTH_LONG).show();
                break;
            default:
                break;
        }


}
}